package server.business.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.business.mapper.BloodInformationMapper;
import server.business.mapper.StateMapper;
import server.business.pojo.BloodInformation;
import server.business.pojo.BloodOutbound;
import server.business.pojo.State;
import server.business.service.BloodInformationService;
import server.business.service.BloodOutboundService;
import server.business.utils.UUIDUtils;
import server.business.vo.Result;
import server.business.vo.ResultExpand;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author Galaxy
 * @description 针对表【blood_information】的数据库操作Service实现
 * @createDate 2022-08-03 13:36:56
 */
@Service("bloodInformationService")
public class BloodInformationServiceImpl extends ServiceImpl<BloodInformationMapper, BloodInformation>
        implements BloodInformationService {

    @Autowired
    private BloodInformationMapper bloodInformationMapper;
    @Autowired
    private BloodInformationService bloodInformationService;
    @Autowired
    private StateMapper stateMapper;
    @Autowired
    private BloodOutboundService bloodOutboundService;

    /**
     * 更改血液信息
     * @param bloodInformation
     */
    @Override
    public void updateBloodInfo(BloodInformation bloodInformation) {
        LambdaUpdateWrapper<BloodInformation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Strings.isNotBlank(bloodInformation.getBloodId()), BloodInformation::getBloodId, bloodInformation.getBloodId());
        updateWrapper.set(Strings.isNotBlank(bloodInformation.getBloodComponent()), BloodInformation::getBloodComponent, bloodInformation.getBloodComponent())
                .set(Strings.isNotBlank(bloodInformation.getAboBloodType()), BloodInformation::getAboBloodType, bloodInformation.getAboBloodType())
                .set(Strings.isNotBlank(bloodInformation.getRhBloodType()), BloodInformation::getRhBloodType, bloodInformation.getRhBloodType())
                .set(bloodInformation.getBloodCount() != null, BloodInformation::getBloodCount, bloodInformation.getBloodCount())
                .set(Strings.isNotBlank(bloodInformation.getBloodSource()), BloodInformation::getBloodSource, bloodInformation.getBloodSource())
                .set(Strings.isNotBlank(bloodInformation.getBloodLocation()), BloodInformation::getBloodLocation, bloodInformation.getBloodLocation())
                .set(Strings.isNotBlank(bloodInformation.getState()), BloodInformation::getState, bloodInformation.getState());
        bloodInformationMapper.update(null, updateWrapper);
    }

    /**
     * 选择页面
     *
     * @param currentPage 当前页面
     * @param limit       限制
     * @return {@link IPage}<{@link BloodInformation}>
     */
    @Override
    public IPage<BloodInformation> selectByPage(int currentPage, int limit) {
        Page<BloodInformation> page = new Page<>(currentPage, limit);
        IPage<BloodInformation> bloodInformationIPage = bloodInformationMapper.selectPage(page, null);
        return bloodInformationIPage;
    }

    /**
     * 查询数据
     *
     * @return {@link List}<{@link BloodInformation}>
     */
    @Override
    public List<BloodInformation> queryData() {
        List<BloodInformation> list = bloodInformationService.list();
        return list;
    }

    /**
     * 获取页面
     *
     * @param request  请求
     * @param response 响应
     * @return {@link ResultExpand}
     */
    @Override
    public ResultExpand getPage(HttpServletRequest request, HttpServletResponse response) {
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        Page<BloodInformation> page = new Page<>(currentPage, pageSize);
        IPage<BloodInformation> bloodInformationIPage = bloodInformationMapper.selectPage(page, null);
        ResultExpand resultExpand = new ResultExpand("200", "成功", bloodInformationIPage.getRecords(), currentPage, pageSize, bloodInformationIPage.getTotal(), bloodInformationIPage.getPages());
        return resultExpand;
    }

    /**
     * 搜索血液信息
     *
     * @param request  请求
     * @param response 响应
     * @return {@link ResultExpand}
     */
    @Override
    public ResultExpand searchBloodInformation(HttpServletRequest request, HttpServletResponse response) {
        String bloodId = request.getParameter("bloodId");
        String barcode = request.getParameter("barcode");
        String extendCode = request.getParameter("extendCode");
        String aboBloodType = request.getParameter("aboBloodType");
        String rhBloodType = request.getParameter("rhBloodType");
        String state = request.getParameter("state");
        String bloodSource = request.getParameter("bloodSource");
        String bloodLocation = request.getParameter("bloodLocation");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        //创建分页对象
//        IPage<BloodInformation> page = new Page<>(currentPage, pageSize);

        //创建查询条件
        QueryWrapper<BloodInformation> queryWrapper = new QueryWrapper<>();
        if (bloodId != "")
            queryWrapper.like("blood_id", bloodId);
        if (barcode != "")
            queryWrapper.like("barcode", barcode);
        if (extendCode != "")
            queryWrapper.like("extend_code", extendCode);
        if (aboBloodType != "")
            queryWrapper.eq("abo_blood_type", aboBloodType);
        if (rhBloodType != "")
            queryWrapper.eq("rh_blood_type", rhBloodType);
        if (state != "")
            queryWrapper.eq("t1.state", state);     //指定是哪个表的staate
        if (bloodSource != "")
            queryWrapper.eq("blood_source", bloodSource);
        if (bloodLocation != "")
            queryWrapper.eq("blood_location", bloodLocation);
        if (startTime != "")
            queryWrapper.ge("failure_time", startTime);
        if (endTime != "")
            queryWrapper.le("failure_time", endTime + " 23:59:59");
        //查询数据库
//        bloodInformationService.page(page, queryWrapper);
        IPage<BloodInformation> page = bloodInformationMapper.selectBloodpage(
                new Page<>(currentPage, pageSize), queryWrapper
        );
        ResultExpand resultExpand = new ResultExpand();
        resultExpand.setCurrent(currentPage).setPageSize(pageSize).setTotal(page.getTotal()).setPages(page.getPages());
        resultExpand.setCode("200");
        resultExpand.setMsg("搜索成功");
        resultExpand.setData(page.getRecords());
        return resultExpand;
    }

    /**
     * 血液出库
     *
     * @return {@link Result}
     */
    @Override
    public Result outboundBlood(HttpServletRequest request, HttpServletResponse response) {
        BloodInformation bloodInformation = JSONObject.parseObject(request.getParameter("bloodInformation"), BloodInformation.class);

        String id = request.getParameter("id");
        String stateInfo = request.getParameter("stateInfo");
        String outboundReason = request.getParameter("outboundReason");
        String outboundLocation = request.getParameter("outboundLocation");
        String bloodTaker = request.getParameter("bloodTaker");
        String bloodTakerNumber = request.getParameter("bloodTakerNumber");
        String operator = request.getParameter("operator");
        String operatorNumber = request.getParameter("operatorNumber");

        //查询id对应的血液
//        BloodInformation bloodInformation = bloodInformationMapper.selectById(id);

        //查询“已出库”状态的代码
        QueryWrapper<State> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", "已出库");
        State state = stateMapper.selectOne(queryWrapper);
        String stateId = state.getCode();

        //存入blood_outBound
        BloodOutbound bloodOutbound = new BloodOutbound();
        bloodOutbound.setGmtCreate(new Date());
        bloodOutbound.setGmtModified(new Date());
        bloodOutbound.setOutboundNumber(UUIDUtils.getUUID());//出库单据号
        bloodOutbound.setBloodInformation(JSON.toJSONString(bloodInformation)); //血液信息
        bloodOutbound.setOutboundReason(outboundReason);    //出库原因
        bloodOutbound.setOutboundTime(new Date());  //出库日期
        bloodOutbound.setOutboundLocation(outboundLocation);    //出库地址
        bloodOutbound.setBloodTaker(bloodTaker);    //取血人
        bloodOutbound.setBloodTakerNumber(bloodTakerNumber);
        bloodOutbound.setOperator(operator);
        bloodOutbound.setOperatorNumber(operatorNumber);
        bloodOutbound.setBloodId(id);

        //存入血液出库表
        boolean successOut = bloodOutboundService.save(bloodOutbound);


        Result result = new Result();
        if ("在库".equals(stateInfo)) {
            UpdateWrapper<BloodInformation> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", id);
            updateWrapper.set("state", stateId);
            bloodInformationMapper.update(null, updateWrapper);
            result.setCode("200");
            result.setMsg("出库成功");
        } else {
            result.setCode("000");
            result.setMsg("出库出错");
        }
        return result;
    }
//    public Result outboundBlood(HttpServletRequest request, HttpServletResponse response) {
//        //查询数据库中的血液信息
//        String id = request.getParameter("id");
//        String stateInfo = request.getParameter("stateInfo");
//        BloodInformation bloodInformation = bloodInformationMapper.selectById(id);
//        //查询“已出库”状态的代码
//        QueryWrapper<State> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("state", "已出库");
//        State state = stateMapper.selectOne(queryWrapper);
//        String stateId = state.getCode();
//
//        Result result = new Result();
//        if ("在库".equals(stateInfo)) {
//            UpdateWrapper<BloodInformation> updateWrapper = new UpdateWrapper<>();
//            updateWrapper.eq("id", id);
//            updateWrapper.set("state", stateId);
//            bloodInformationMapper.update(null, updateWrapper);
//            result.setCode("200");
//            result.setMsg("出库成功");
//        }
//        else {
//            result.setCode("000");
//            result.setMsg("出库出错");
//        }
//        return result;
//    }


    /**
     * 导出血液信息
     *
     * @param request  请求
     * @param response 响应
     */
    @Override
    public void downloadExcel(HttpServletRequest request, HttpServletResponse response) {
        //接收参数
        String bloodId = request.getParameter("bloodId");
        String barcode = request.getParameter("barcode");
        String extendCode = request.getParameter("extendCode");
        String aboBloodType = request.getParameter("aboBloodType");
        String rhBloodType = request.getParameter("rhBloodType");
        String state = request.getParameter("state");
        String bloodSource = request.getParameter("bloodSource");
        String bloodLocation = request.getParameter("bloodLocation");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        try {
            String filename = "血液信息";
            String userAgent = request.getHeader("User-Agent");
            if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
                filename = URLEncoder.encode(filename, "UTF-8");
            } else {
                filename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
            }
            response.setContentType("application/json.ms-exce");
            response.setCharacterEncoding("utf-8");
            response.addHeader("Content-Disposition", "filename = " + filename + ".xlsx");
            //创建查询条件
            QueryWrapper<BloodInformation> queryWrapper = new QueryWrapper<>();
            if (bloodId != "")
                queryWrapper.like("blood_id", bloodId);
            if (barcode != "")
                queryWrapper.like("barcode", barcode);
            if (extendCode != "")
                queryWrapper.like("extend_code", extendCode);
            if (aboBloodType != "")
                queryWrapper.eq("abo_blood_type", aboBloodType);
            if (rhBloodType != "")
                queryWrapper.eq("rh_blood_type", rhBloodType);
            if (state != "")
                queryWrapper.eq("state", state);
            if (bloodSource != "")
                queryWrapper.eq("blood_source", bloodSource);
            if (bloodLocation != "")
                queryWrapper.eq("blood_location", bloodLocation);
            if (startTime != "")
                queryWrapper.ge("failure_time", startTime);
            if (endTime != "")
                queryWrapper.le("failure_time", endTime + " 23:59:59");
            List<BloodInformation> bloodInformationList = bloodInformationService.list(queryWrapper);
            EasyExcel.write(response.getOutputStream(), BloodInformation.class).sheet("sheet").doWrite(bloodInformationList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * @return
     */
    @Override
    public List<BloodInformation> getAll() {
        return this.list();
    }


    /**
     * 根绝血液编号更改血液状态
     *
     * @param bloodNumber
     * @param
     */

    @Override
    public void setStateByBloodNumber(String bloodNumber, String state) {
        LambdaUpdateWrapper<BloodInformation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BloodInformation::getBloodId, bloodNumber);
        updateWrapper.set(BloodInformation::getState, state);
        int result = bloodInformationMapper.update(null, updateWrapper);
        System.out.println("result" + result);
    }

    /**
     * 根据血液编号更改血液信息
     */
    @Override
    public void editDataByBloodNumber(BloodInformation bloodInformation) {
        LambdaUpdateWrapper<BloodInformation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Strings.isNotBlank(bloodInformation.getBloodId()),
                BloodInformation::getBloodId, bloodInformation.getBloodId());
        updateWrapper.set(bloodInformation.getBloodCount() != null,
                BloodInformation::getBloodCount, bloodInformation.getBloodCount())
                .set(Strings.isNotBlank(bloodInformation.getAboBloodType()),
                        BloodInformation::getAboBloodType, bloodInformation.getAboBloodType())
                .set(Strings.isNotBlank(bloodInformation.getRhBloodType()),
                        BloodInformation::getRhBloodType, bloodInformation.getRhBloodType())
                .set(Strings.isNotBlank(bloodInformation.getState()), BloodInformation::getState,
                        bloodInformation.getState())
                .set(Strings.isNotBlank(bloodInformation.getBloodLocation()),
                        BloodInformation::getBloodLocation, bloodInformation.getBloodLocation())
                .set(bloodInformation.getBloodAmount() != null,
                        BloodInformation::getBloodAmount, bloodInformation.getBloodAmount());
        int result = bloodInformationMapper.update(null, updateWrapper);
        System.out.println("result" + result);
    }

    /**
     * 根据血液Id设置血液状态
     *
     * @param bloodId
     * @param
     */
    @Override
    public void setStateByBloodId(Long bloodId, String state) {
        LambdaUpdateWrapper<BloodInformation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(bloodId != null, BloodInformation::getId, bloodId);
        updateWrapper.set(BloodInformation::getState, state);
        bloodInformationMapper.update(null, updateWrapper);
    }


    /**
     * fzx
     *
     * @param BloodId
     * @return
     */
    @Override
    public HashMap<String, Object> getFrom(String BloodId) {
        LambdaQueryWrapper<BloodInformation> bloodSource = new LambdaQueryWrapper<>();
        bloodSource.eq(BloodInformation::getBloodId, BloodId)
                .select(BloodInformation::getCollectTime,
                        BloodInformation::getBloodSource,
                        BloodInformation::getState,
                        BloodInformation::getBloodComponent,
                        BloodInformation::getBloodCount,
                        BloodInformation::getAboBloodType,
                        BloodInformation::getRhBloodType,
                        BloodInformation::getBloodLocation);
        if (this.listMaps(bloodSource).isEmpty()) {
            return null;
        }
        ;
        HashMap<String, Object> map = (HashMap<String, Object>) this.listMaps(bloodSource).get(0);
        System.out.println(map);

        return map;
    }


    // *************************************lrz*************************start

    /**
     * 分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param bloodInformation
     * @return
     */
    @Override
    @SuppressWarnings("")
    public IPage<BloodInformation> getPage(int currentPage, int pageSize, BloodInformation bloodInformation) {
        LambdaQueryWrapper<BloodInformation> queryWrapper = new LambdaQueryWrapper<BloodInformation>();
        queryWrapper
                .eq(Strings.isNotBlank(bloodInformation.getBloodId()),
                        BloodInformation::getBloodId,
                        bloodInformation.getBloodId())
                .eq(Strings.isNotBlank(bloodInformation.getState()), BloodInformation::getState,
                        bloodInformation.getState())
                .eq(Strings.isNotBlank(bloodInformation.getAboBloodType()),
                        BloodInformation::getAboBloodType,
                        bloodInformation.getAboBloodType())
                .eq(Strings.isNotBlank(bloodInformation.getRhBloodType()),
                        BloodInformation::getRhBloodType,
                        bloodInformation.getRhBloodType())
                .eq(Strings.isNotBlank(bloodInformation.getBloodComponent()),
                        BloodInformation::getBloodComponent,
                        bloodInformation.getBloodComponent())
                .eq(bloodInformation.getBloodCount() != null,
                        BloodInformation::getBloodCount,
                        bloodInformation.getBloodCount())
                .eq(Strings.isNotBlank(bloodInformation.getBloodSource()),
                        BloodInformation::getBloodSource,
                        bloodInformation.getBloodSource())
                .eq(Strings.isNotBlank(bloodInformation.getBloodLocation()),
                        BloodInformation::getBloodLocation,
                        bloodInformation.getBloodLocation())
                .le(bloodInformation.getCollectTime() != null, BloodInformation::getCollectTime,
                        bloodInformation.getCollectTime())
                .ge(bloodInformation.getFailureTime() != null, BloodInformation::getFailureTime,
                        bloodInformation.getFailureTime());
        IPage<BloodInformation> page = new Page<>(currentPage, pageSize);
        bloodInformationMapper.selectPage(page, queryWrapper);
        return page;
    }


    /**
     * 根据输血申请单信息获取在库的血液信息
     *
     * @param
     * @return
     */
    @Override
    public List<BloodInformation> getBloodInStockByTransfusion(BloodInformation bloodInformation) {
        LambdaQueryWrapper<BloodInformation> queryWrapper = new LambdaQueryWrapper<BloodInformation>();
        queryWrapper
                .eq(Strings.isNotBlank(bloodInformation.getAboBloodType()),
                        BloodInformation::getAboBloodType,
                        bloodInformation.getAboBloodType())
                .eq(Strings.isNotBlank(bloodInformation.getRhBloodType()),
                        BloodInformation::getRhBloodType,
                        bloodInformation.getRhBloodType())
                .eq(Strings.isNotBlank(bloodInformation.getBloodComponent()),
                        BloodInformation::getBloodComponent,
                        bloodInformation.getBloodComponent())
                .ge(bloodInformation.getBloodCount() != null,
                        BloodInformation::getBloodCount,
                        bloodInformation.getBloodCount())
                .eq(BloodInformation::getState, "在库");
        queryWrapper.select().orderByAsc(BloodInformation::getFailureTime);
        List<BloodInformation> list = bloodInformationMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public BloodInformation getSingleBloodInStockByTransfusion(BloodInformation bloodInformation) {
        LambdaQueryWrapper<BloodInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Strings.isNotBlank(bloodInformation.getAboBloodType()),
                        BloodInformation::getAboBloodType,
                        bloodInformation.getAboBloodType())
                .eq(Strings.isNotBlank(bloodInformation.getRhBloodType()),
                        BloodInformation::getRhBloodType,
                        bloodInformation.getRhBloodType())
                .eq(Strings.isNotBlank(bloodInformation.getBloodComponent()),
                        BloodInformation::getBloodComponent,
                        bloodInformation.getBloodComponent())
                .ge(bloodInformation.getBloodCount() != null,
                        BloodInformation::getBloodCount,
                        bloodInformation.getBloodCount())
                .le(bloodInformation.getCollectTime() != null, BloodInformation::getCollectTime, bloodInformation.getCollectTime())
                .ge(bloodInformation.getFailureTime() != null, BloodInformation::getFailureTime, bloodInformation.getFailureTime())
                .eq(BloodInformation::getState, "2"); // 2代表在库
        queryWrapper.select().orderByDesc(BloodInformation::getFailureTime);
        queryWrapper.last("limit 0,1");
        BloodInformation result = bloodInformationMapper.selectOne(queryWrapper);
        return result;
    }


    /**
     * 根据输血申请单信息获取在库的血液信息
     */
    @Override
    public List<BloodInformation> reAssignSingleBloodInStockByTransfusion(BloodInformation bloodInformation) {
        LambdaQueryWrapper<BloodInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Strings.isNotBlank(bloodInformation.getAboBloodType()),
                        BloodInformation::getAboBloodType,
                        bloodInformation.getAboBloodType())
                .eq(Strings.isNotBlank(bloodInformation.getRhBloodType()),
                        BloodInformation::getRhBloodType,
                        bloodInformation.getRhBloodType())
                .eq(Strings.isNotBlank(bloodInformation.getBloodComponent()),
                        BloodInformation::getBloodComponent,
                        bloodInformation.getBloodComponent())
                .ge(bloodInformation.getBloodCount() != null,
                        BloodInformation::getBloodCount,
                        bloodInformation.getBloodCount())
                .eq(BloodInformation::getState, "2"); // 2代表在库
        // queryWrapper.notIn(BloodInformation::getBloodId, bloodIdList);
        queryWrapper.select().orderByAsc(BloodInformation::getFailureTime);
        // queryWrapper.last("limit 0,1");
        List<BloodInformation> result = bloodInformationMapper.selectList(queryWrapper);
        return result;
    }


    /**
     * 根据血液ID获取血液信息
     *
     * @param bloodInformation
     * @return
     */
    @Override
    public BloodInformation getBloodInfo(BloodInformation bloodInformation) {
        LambdaQueryWrapper<BloodInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(bloodInformation.getBloodId() != null, BloodInformation::getBloodId,
                bloodInformation.getBloodId());
        BloodInformation result = bloodInformationMapper.selectOne(queryWrapper);
        return result;
    }


    /**
     * 根据血液Id设置血液状态
     *
     * @param bloodId
     * @param
     */
    @Override
    public void setStateByBloodId(String bloodId, String state) {
        LambdaUpdateWrapper<BloodInformation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Strings.isNotBlank(bloodId), BloodInformation::getBloodId, bloodId);
        updateWrapper.set(BloodInformation::getState, state);
        bloodInformationMapper.update(null, updateWrapper);
    }

    /**
     * 根据编号批量下载选中数据
     *
     * @param response
     * @param ids
     */
    @Override
    public void batchDownload(HttpServletResponse response, ArrayList<Long> ids) throws IOException {
        List<BloodInformation> bloodInformations = bloodInformationMapper.selectBatchIds(ids);
        // 设置文本内省
        try {
            response.setContentType("application/vnd.ms-excel");
            String fileName = URLEncoder.encode("血液信息" + System.currentTimeMillis(), "UTF-8");
            // 设置字符编码
            response.setCharacterEncoding("utf-8");
            // 设置响应头
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(),
                    BloodInformation.class).sheet("血液信息")
                    .doWrite(bloodInformations);
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //*************************************lrz*************************end


    //*************************************zy*************************start


    /**
     * 血站在库血液信息查询模块
     *
     * @return 返回血站在库血液信息列表
     */
    @Override
    public List<BloodInformation> findStationBloodInformation() {
        return bloodInformationMapper.findStationBloodInformation();
    }

    /**
     * 根据血液Id设置血液状态
     *
     * @param bloodNumber 血液Id
     * @param condition   待设置的血液状态
     */
    @Override
    public void setConditionByBloodNumber(String bloodNumber, String condition) {
        bloodInformationMapper.setConditionByBloodNumber(bloodNumber, condition);
    }

    /**
     * 根据编号批量下载选中数据
     *
     * @param response 请求
     * @param ids      编号列表
     */
    @Override
    public void batchDownloadPba(HttpServletResponse response, ArrayList<Long> ids) throws IOException {
        List<BloodInformation> bloodInformations = bloodInformationMapper.selectBatchIds(ids);
        // 设置文本内容
        try {
            response.setContentType("application/vnd.ms-excel");
            String fileName = URLEncoder.encode("血站退血申请信息" + System.currentTimeMillis(), "UTF-8");
            // 设置字符编码
            response.setCharacterEncoding("utf-8");
            // 设置响应头
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcelFactory.write(response.getOutputStream(),
                    BloodInformation.class).sheet("血站退血申请信息")
                    .doWrite(bloodInformations);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 血站退血申请分页
     *
     * @param currentPage      当前页面
     * @param pageSize         当前页面大小
     * @param bloodInformation 血液信息实体类
     * @return 分页结果
     */
    @Override
    public IPage<BloodInformation> getPageBi(int currentPage, int pageSize, BloodInformation bloodInformation) {
        LambdaQueryWrapper<BloodInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(bloodInformation.getBarcode() != null, BloodInformation::getBarcode, bloodInformation.getBarcode())
                .eq(Strings.isNotBlank(bloodInformation.getExtendCode()), BloodInformation::getExtendCode, bloodInformation.getExtendCode())
                .eq(Strings.isNotBlank(bloodInformation.getBloodComponent()), BloodInformation::getBloodComponent, bloodInformation.getBloodComponent())
                .eq(Strings.isNotBlank(bloodInformation.getAboBloodType()), BloodInformation::getAboBloodType, bloodInformation.getAboBloodType())
                .eq(Strings.isNotBlank(bloodInformation.getRhBloodType()), BloodInformation::getRhBloodType, bloodInformation.getRhBloodType())
                .like(bloodInformation.getFailureTime() != null, BloodInformation::getFailureTime, bloodInformation.getFailureTime())
                .like(bloodInformation.getCollectTime() != null, BloodInformation::getCollectTime, bloodInformation.getCollectTime())
                .eq(Strings.isNotBlank(bloodInformation.getBloodSource()), BloodInformation::getBloodSource, bloodInformation.getBloodSource())
                .eq(Strings.isNotBlank(bloodInformation.getState()), BloodInformation::getState, bloodInformation.getState())
                .eq(bloodInformation.getInboundAmount() != null, BloodInformation::getInboundAmount, bloodInformation.getInboundAmount())
                .eq(Strings.isNotBlank(bloodInformation.getBloodId()), BloodInformation::getBloodId, bloodInformation.getBloodId())
                .eq(Strings.isNotBlank(bloodInformation.getBloodLocation()), BloodInformation::getBloodLocation, bloodInformation.getBloodLocation())
                .eq(bloodInformation.getBloodAmount() != null, BloodInformation::getBloodAmount, bloodInformation.getBloodAmount());
        Page<BloodInformation> page = new Page<>(currentPage, pageSize);
        bloodInformationMapper.selectPage(page, queryWrapper);
        return page;
    }

    /**
     * 根据编号删除
     *
     * @param ids 待删除数据的编号
     * @return 返回删除结果
     */
    @Override
    public boolean deleteBatch(ArrayList<Long> ids) {
        boolean res = bloodInformationService.removeByIds(ids);
        return res;
    }


    //*************************************zy*************************end

    // *************************************ysl********************start

    @Override
    public List<BloodInformation> getBloodByBloodInformation(BloodInformation bloodInformation) {
        LambdaQueryWrapper<BloodInformation> queryWrapper = new LambdaQueryWrapper<BloodInformation>();
        queryWrapper
                .eq(Strings.isNotBlank(bloodInformation.getAboBloodType()),
                        BloodInformation::getAboBloodType,
                        bloodInformation.getAboBloodType())
                .eq(Strings.isNotBlank(bloodInformation.getRhBloodType()),
                        BloodInformation::getRhBloodType,
                        bloodInformation.getRhBloodType())
                .eq(Strings.isNotBlank(bloodInformation.getBloodComponent()),
                        BloodInformation::getBloodComponent,
                        bloodInformation.getBloodComponent())
                .eq(BloodInformation::getBloodCount,bloodInformation.getBloodCount())
                .eq(BloodInformation::getState, "2");//2:在库
        queryWrapper.select().orderByAsc(BloodInformation::getFailureTime);
        List<BloodInformation> list = bloodInformationMapper.selectList(queryWrapper);
        return list;
    }


    // *************************************ysl********************end


}


